var Child = {
	template: '<div class="noMsg"><img class="noMsg-svg" src="../../images/noMsg.png"><p class="noMsg-p">暂无信息</p></div>'
};
new Vue({
	el: '#app',
	data: {
		uid: '',
		cid: '',
		isShow: false,
		classList: [],
		t: [],
		page: '1',
		url: '',
		categoryList: [],
		cateH: '',
		isNoM: false
	},
	mounted: function() {
		console.log("mounted end");
		this.init();
	},
	components: {
		// <my-component> 将只在父模板可用
		'my-component': Child
	},
	methods: {
		init: function() {
			var self = this;
			//self.category();
			//self.getClass();
			apiready = function() {
				//loading加载
				self.isS();
				//滚动到底部加载更多
				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						if(self.cid) {
							self.getClass();
						} else {
							//							self.category();
						}
					}
				});
			}
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				if(self.cid) {
					self.getClass();
				} else {
					self.category();
				}

			}
		},
		//获取课程分类
		category: function() {
			var self = this;
			$.get(base + config.study_category_list, function(data) {
				console.log(data);
				if(data.error == 0) {
					self.categoryList = data.data;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						self.cateH = $("#cate").height();
					});
					self.getClass();
				} else {
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//选择分类
		choose: function(cid) {
			var self = this;
			self.cid = cid;
			self.classList = [];
			self.page = '1';
			self.isShow = false;
			self.isS();
		},
		//是否有数据
		isNoMsg: function(d) {
			console.log(d);
			if(d.length == 0) {
				this.isNoM = true;
			}else{
				this.isNoM = false;
			}
		},
		//获得课程的列表
		getClass: function() {
			var self = this;
			console.log(self.cid);
			var data = {
				page: self.page,
				category_id: self.cid
			};
			$.post(base + config.study_list, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.data;
					for(var i = 0; i < self.t.length; i++) {
						self.classList.push(self.t[i]);
					};
					self.pageCount = msg.page_count;
					self.url = msg.url;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						console.log(self.cateH);
						$("#main").css("margin-top", (self.cateH + 75 + 20) + "px");
					});
					self.isNoMsg(self.classList);
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//关闭本页面
		close: function() {
			api.closeWin();
		},
		//跳转到课程信息
		jumpProductMsg: function(id) {
			api.openWin({
				name: 'productMsg',
				url: 'productMsg.html',
				vScrollBarEnabled: false,
				reload: true,
				pageParam: {
					id: id
				}
			});
		}
	}
})